<?php
/**
 * @package Autogenerator for Joomla!
 * @version 3.0.0
 * @author Truong Cong Duc <truongcongduc4@gmail.com>
 * @link autogenerator.blogspot.com
 * @copyright (c) 2013, Truong Cong Duc
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

require_once JPATH_COMPONENT_ADMINISTRATOR . '/helpers/autogenerator.php';

class AutogeneratorModelMvc extends JModelAdmin
{

    protected $text_prefix = 'COM_AUTOGENERATOR';

    public function getForm($data = array(), $loadData = true)
    {
        $form = $this->loadForm('com_autogenerator.mvc', 'mvc', array('control' => 'jform', 'load_data' => $loadData));

        if (empty($form))
        {
            return false;
        }

        return $form;
    }

    public function getTable($type = 'Mvc', $prefix = 'AutogeneratorTable', $config = array())
    {
        return JTable::getInstance($type, $prefix, $config);
    }

    protected function loadFormData()
    {
        $data = JFactory::getApplication()->getUserState('com_autogenerator.edit.mvc.data', array());

        if (empty($data))
        {
            $data = $this->getItem();
        }

        return $data;
    }

    protected function getReorderConditions($table)
    {
        $condition = array();

        return $condition;
    }

    protected function prepareTable($table)
    {
        
    }

    public function storeTable($aData) {
        $oTable = $this->getTable();

        $sKey = $oTable->getKeyName();

        $iPrimaryKey = (!empty($aData[$sKey])) ? $aData[$sKey] : 0;

        $bIsNew = true;

        // Allow an exception to be thrown.
        try {
            // Load the row if saving an existing record.
            if ($iPrimaryKey > 0) {
                $oTable->load($iPrimaryKey);

                $bIsNew = false;
            }

            // Bind the data.
            if (!$oTable->bind($aData)) {
                $this->setError($oTable->getError());

                return false;
            }

            // Check the data.
            if (!$oTable->check()) {
                $this->setError($oTable->getError());
                return false;
            }

            // Store the data.
            if (!$oTable->store()) {
                $this->setError($oTable->getError());
                return false;
            }
        } catch (Exception $oException) {
            $this->setError($oException->getMessage());

            return false;
        }

        return true;
    }
    
    public function deleteAllMVC($iTableId) {
        $oDatabase = JFactory::getDbo();

        $oQuery = $oDatabase->getQuery(true);
        
        $oQuery->delete($oDatabase->quoteName('#__auto_mvc'))
                ->where('table_id = ' . (int) $iTableId);

        $oDatabase->setQuery($oQuery);
        try {
            $bResult = $oDatabase->query();
        } catch (Exception $e) {
            if ($sError = $oDatabase->getErrorMsg()) {
                return $sError;
            }
        }
        return $bResult;
    }
    
    public function getDrapMVC($iTableId)
    {
        $oModelTable = JModelLegacy::getInstance('Table', 'AutogeneratorModel');
        $oTable = $oModelTable->getItem($iTableId);
        if ($oTable->id < 1)
        {
            return array();
        }
        return array(
            array(
                'table_id' => $iTableId,
                'title' => $oTable->title . '_form',
                'type' => ConstantHelper::$_MVC_TYPE_FORM
            ),
            array(
                'table_id' => $iTableId,
                'title' => $oTable->title . '_list',
                'type' => ConstantHelper::$_MVC_TYPE_LIST
            ),
            array(
                'table_id' => $iTableId,
                'title' => $oTable->title . '_item',
                'type' => ConstantHelper::$_MVC_TYPE_ITEM
            )
        );
        
    }
}
